import com.thoughtworks.selenium.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Pattern;

	public class test2 {
	private Selenium selenium;
		String vResult;String vTax; String vPMI;String xpath; String xPath_result;
		static String[][] xData;static int xCols; static int xRows;

		@Before
		public void setUp() throws Exception {
			String xPath="C:/Users/chitra/sele lib/MCexcel.xls";
			xlread(xPath);
			
			selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.mortgagecalculator.org/");
			selenium.start();
		}

		@Test
		public void testUntitled() throws Exception {
			String vExecute;
						int i;
			for(i=1; i<xRows;i++){
			
			String vLoan =xData[1][0];
			String vCredit="label=Good";
			String vTerm =xData[i][1];
			String vRate =xData[i][2];
			 vTax =xData[i][5];
			 vPMI = xData[i][6];
			 vExecute= xData[i][10];
			 
			System.out.println("loan value is: "+vLoan);
			System.out.println("tax is "+vTax);
			System.out.println("PMI  is "+vPMI);
			if(vExecute.equals("y")){
			//for(int i=0; i<3; i++){
			
			//vRate=Integer.toString((Integer.parseInt(vRate)-1));
			selenium.open("http://www.mortgagecalculator.org/");
			selenium.type("name=param[principal]", vLoan);
			selenium.type("name=param[term]", vTerm);
			selenium.type("name=param[interest_rate]", vRate);
			selenium.type("name=param[property_tax]", vTax);
			selenium.type("name=param[pmi]", vPMI); 
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			String mc1 =selenium.getText("css=td>h3");
			System.out.println("value of mc1 " +mc1);
			xData[i][7] =mc1;
			
			selenium.open("http://www.mortgagecalculatorplus.com/");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=param[principal]", vLoan);
			selenium.type("name=param[interest_rate]", vRate);
			selenium.type("name=param[term]",vTerm);
			selenium.type("name=param[property_tax]", vTax);
			selenium.type("name=param[pmi]", vTax); 
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			String mc2 =selenium.getText("css=h3");
			System.out.println("value of mc2 " +mc2); 	
			vResult= mycompare(mc1,mc2);
			xData[i][9]=vResult;
			System.out.println("the result for test" +i +"is "+vResult) ;
			xData[i][8]=mc2;
		}else
		{
			System.out.println("row#" +i +"is not executed");
		}
			}
		}
		@After
	public void tearDown() throws Exception {
			String xPath_result ="C:/Users/chitra/sele lib/MCexcel-result.xls";
			xlwrite(xPath_result,xData);
			
			System.out.println("done");
		}
			
	public String myprint2() {
		System.out.println("my fmag does that");
		return("done");
	}
	public String mycompare(String text1, String text2){
	if(text1.equals(text2)){
		System.out.println("test is pass");
		return "pass";
		}
	else{
		System.out.println("test is fail");
		return "fail";
			}

	}
	public  void xlread(String MCexcel) throws Exception{
        File myxl =new File(MCexcel);   
        FileInputStream FIS = new FileInputStream(myxl);
        HSSFWorkbook WB = new HSSFWorkbook(FIS);
        HSSFSheet mySheet = WB.getSheetAt(0);
        xRows=mySheet.getLastRowNum() +1;
        xCols=mySheet.getRow(0).getLastCellNum();
        System.out.println("Rows are "+ xRows);
        System.out.println("column are "+ xCols);
        xData=new String[xRows][xCols];
	    
	for(int i=0;i<xRows; i++){
		HSSFRow row=mySheet.getRow(i);
		 for (short j=0; j<xCols; j++){
		 HSSFCell cell=row.getCell(j);// to read the value of each cell
		 String value =cellToString(cell);
		 xData[i][j]= value;
		 
		 }
	}
	}
	public static String cellToString(HSSFCell cell){
		int type = cell.getCellType();
		Object result;
	switch (type){
	case HSSFCell.CELL_TYPE_NUMERIC:
		result = cell.getNumericCellValue();
		break;
	case HSSFCell.CELL_TYPE_STRING:
		result =cell.getStringCellValue();
		break;
	case HSSFCell.CELL_TYPE_BLANK:
		result="";
		break;
	case HSSFCell.CELL_TYPE_FORMULA:
		throw new RuntimeException("no formula");
	case HSSFCell.CELL_TYPE_BOOLEAN:
		result=cell.getBooleanCellValue();
		break;
	case HSSFCell.CELL_TYPE_ERROR:
		throw new RuntimeException("this cell has an error");
		default:
			throw new RuntimeException("we dont support this cell type:  "+type);
	}
	return result.toString();
	}		
 
         
    public static void xlwrite(String xlpath, String[][]xldata) throws IOException {
       File outfile =new File(xlpath);
            HSSFWorkbook WB = new HSSFWorkbook();
            HSSFSheet mySheet = WB.createSheet("Test-Result");
               for (int myrow = 0; myrow < xRows; myrow++) {
             HSSFRow row = mySheet.createRow(myrow);
                for (int mycol = 0; mycol < xCols; mycol++) {
                    HSSFCell cell = row.createCell(mycol);
                    cell.setCellValue(xldata[myrow][mycol]);
                    
                }
            }


            FileOutputStream out = new FileOutputStream(xlpath);
            WB.write(out);
            out.flush();
            out.close();
            

        } 

		

		
		
	}
	


	
